<script setup>
import { computed, ref } from 'vue'
import SvgIcon from '../components/SvgIcon.vue'
import { useUserStore } from '../store'

const loginForm = ref({
    username: 'admin',
    password: '123456'
})

const rules = {
    username: [
        {
            required: true,
            message: '请输入账号',
            trigger: 'change'
        }
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: 'change'
        }
    ]

}
// 表单信息
const formRef = ref(null)
// 密码框状态
const isShow = ref(false)
const showIcon = computed(() => isShow.value ? 'eye' : 'eye-close')
const passOrText = computed(() => isShow.value ? 'text' : 'password')

const userStore = useUserStore()

// 登录
const handleLogin = async () => {
    formRef.value.validate((valid) => {
        if (valid) {
            // 登录
            userStore.login(loginForm.value)
        } else {
            return false
        }
    })
}

</script>

<template>
    <div class="login-container">
        <el-form ref="formRef" :model="loginForm" class="login-form" autocomplete="on" :rules="rules">
            <div class="title-container">
                <h3 class="title">用户登录</h3>
            </div>
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" clearable>
                    <template #prefix>
                        <svg-icon name="user"></svg-icon>
                    </template>
                </el-input>
            </el-form-item>

            <el-form-item prop="password">
                <el-input :type="passOrText" v-model="loginForm.password">
                    <template #prefix>
                        <svg-icon name="lock"></svg-icon>
                    </template>
                    <template #suffix>
                        <svg-icon @click="() => isShow = !isShow" style="cursor: pointer; user-select: none;"
                            :name="showIcon"></svg-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form>
    </div>
</template>

<style scoped>
.login-container {
    background-image: linear-gradient(to top, #a8edea 0%, #fed6e3 100%);
    color: white;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-form {
    width: 40%;
    padding: 40px;
    border-radius: 6px;
    box-shadow: 1px 1px 5px rgba(255, 255, 255, 1),
        1px 1px 5px rgba(0, 0, 0, 0.1);
}

.title-container {
    text-align: center;
    color: #409eff;
}

.title {
    padding: 0 0 20px 0;
}

.login-button {
    width: 100%;
}
</style>